﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.DefaultSettingsProvider
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using BUTR.DependencyInjection.Logger;
using MCM.Abstractions;
using MCM.Abstractions.Base;
using MCM.Abstractions.Base.Global;
using MCM.Abstractions.Base.PerCampaign;
using MCM.Abstractions.Base.PerSave;
using MCM.Abstractions.Global;
using MCM.Abstractions.PerCampaign;
using MCM.Abstractions.PerSave;
using System;
using System.Collections.Generic;
using System.Linq;

#nullable enable
namespace MCM.Implementation
{
  internal sealed class DefaultSettingsProvider : BaseSettingsProvider
  {
    private readonly IBUTRLogger _logger;
    private readonly List<ISettingsContainer> _settingsContainers;
    private readonly List<IExternalSettingsProvider> _externalSettingsProviders;

    public override IEnumerable<SettingsDefinition> SettingsDefinitions
    {
      get
      {
        return this._settingsContainers.OfType<ISettingsContainerHasSettingsDefinitions>().SelectMany<ISettingsContainerHasSettingsDefinitions, SettingsDefinition>((Func<ISettingsContainerHasSettingsDefinitions, IEnumerable<SettingsDefinition>>) (sp => sp.SettingsDefinitions)).Concat<SettingsDefinition>(this._externalSettingsProviders.SelectMany<IExternalSettingsProvider, SettingsDefinition>((Func<IExternalSettingsProvider, IEnumerable<SettingsDefinition>>) (x => x.SettingsDefinitions)));
      }
    }

    public DefaultSettingsProvider(IBUTRLogger<DefaultSettingsProvider> logger)
    {
      this._logger = (IBUTRLogger) logger;
      List<IGlobalSettingsContainer> list1 = ((IEnumerable<IGlobalSettingsContainer>) ((object) GenericServiceProvider.GetService<IEnumerable<IGlobalSettingsContainer>>() ?? (object) Array.Empty<IGlobalSettingsContainer>())).ToList<IGlobalSettingsContainer>();
      List<IPerSaveSettingsContainer> list2 = ((IEnumerable<IPerSaveSettingsContainer>) ((object) GenericServiceProvider.GetService<IEnumerable<IPerSaveSettingsContainer>>() ?? (object) Array.Empty<IPerSaveSettingsContainer>())).ToList<IPerSaveSettingsContainer>();
      List<IPerCampaignSettingsContainer> list3 = ((IEnumerable<IPerCampaignSettingsContainer>) ((object) GenericServiceProvider.GetService<IEnumerable<IPerCampaignSettingsContainer>>() ?? (object) Array.Empty<IPerCampaignSettingsContainer>())).ToList<IPerCampaignSettingsContainer>();
      List<IExternalSettingsProvider> list4 = ((IEnumerable<IExternalSettingsProvider>) ((object) GenericServiceProvider.GetService<IEnumerable<IExternalSettingsProvider>>() ?? (object) Array.Empty<IExternalSettingsProvider>())).ToList<IExternalSettingsProvider>();
      foreach (IGlobalSettingsContainer settingsContainer in list1)
      {
        if (settingsContainer is ISettingsContainerCanInvalidateCache canInvalidateCache)
        {
          // ISSUE: reference to a compiler-generated field
          // ISSUE: reference to a compiler-generated field
          Action action = DefaultSettingsProvider.\u003C\u003EO.\u003C0\u003E__InvalidateCache ?? (DefaultSettingsProvider.\u003C\u003EO.\u003C0\u003E__InvalidateCache = new Action(GlobalSettings.InvalidateCache));
          canInvalidateCache.InstanceCacheInvalidated += action;
        }
        logger.LogInformation(string.Format("Found Global container {0} ({1})", (object) settingsContainer.GetType(), (object) settingsContainer.SettingsDefinitions.Count<SettingsDefinition>()));
      }
      foreach (IPerSaveSettingsContainer settingsContainer in list2)
      {
        ISettingsContainerCanInvalidateCache canInvalidateCache = (ISettingsContainerCanInvalidateCache) settingsContainer;
        if (canInvalidateCache != null)
        {
          // ISSUE: reference to a compiler-generated field
          // ISSUE: reference to a compiler-generated field
          canInvalidateCache.InstanceCacheInvalidated += DefaultSettingsProvider.\u003C\u003EO.\u003C1\u003E__InvalidateCache ?? (DefaultSettingsProvider.\u003C\u003EO.\u003C1\u003E__InvalidateCache = new Action(PerSaveSettings.InvalidateCache));
        }
        logger.LogInformation(string.Format("Found PerSave container {0} ({1})", (object) settingsContainer.GetType(), (object) settingsContainer.SettingsDefinitions.Count<SettingsDefinition>()));
      }
      foreach (IPerCampaignSettingsContainer settingsContainer in list3)
      {
        ISettingsContainerCanInvalidateCache canInvalidateCache = (ISettingsContainerCanInvalidateCache) settingsContainer;
        if (canInvalidateCache != null)
        {
          // ISSUE: reference to a compiler-generated field
          // ISSUE: reference to a compiler-generated field
          canInvalidateCache.InstanceCacheInvalidated += DefaultSettingsProvider.\u003C\u003EO.\u003C2\u003E__InvalidateCache ?? (DefaultSettingsProvider.\u003C\u003EO.\u003C2\u003E__InvalidateCache = new Action(PerCampaignSettings.InvalidateCache));
        }
        logger.LogInformation(string.Format("Found Campaign container {0} ({1})", (object) settingsContainer.GetType(), (object) settingsContainer.SettingsDefinitions.Count<SettingsDefinition>()));
      }
      foreach (IExternalSettingsProvider settingsProvider in list4)
      {
        if (settingsProvider is IExternalSettingsProviderCanInvalidateCache canInvalidateCache)
          canInvalidateCache.InstanceCacheInvalidated += (Action<ExternalSettingsProviderInvalidateCacheType>) (cacheType =>
          {
            switch (cacheType)
            {
              case ExternalSettingsProviderInvalidateCacheType.Global:
                GlobalSettings.InvalidateCache();
                break;
              case ExternalSettingsProviderInvalidateCacheType.PerCampaign:
                PerCampaignSettings.InvalidateCache();
                break;
              case ExternalSettingsProviderInvalidateCacheType.PerSave:
                PerSaveSettings.InvalidateCache();
                break;
            }
          });
        logger.LogInformation(string.Format("Found external provider {0} ({1})", (object) settingsProvider.GetType(), (object) settingsProvider.SettingsDefinitions.Count<SettingsDefinition>()));
      }
      this._settingsContainers = Enumerable.Empty<ISettingsContainer>().Concat<ISettingsContainer>((IEnumerable<ISettingsContainer>) list1).Concat<ISettingsContainer>((IEnumerable<ISettingsContainer>) list2).Concat<ISettingsContainer>((IEnumerable<ISettingsContainer>) list3).ToList<ISettingsContainer>();
      this._externalSettingsProviders = list4.ToList<IExternalSettingsProvider>();
    }

    public override BaseSettings? GetSettings(string id)
    {
      foreach (ISettingsContainer settingsContainer in this._settingsContainers)
      {
        BaseSettings settings = settingsContainer.GetSettings(id);
        if (settings != null)
          return settings;
      }
      foreach (IExternalSettingsProvider settingsProvider in this._externalSettingsProviders)
      {
        BaseSettings settings = settingsProvider.GetSettings(id);
        if (settings != null)
          return settings;
      }
      this._logger.LogWarning("GetSettings " + id + " returned null");
      return (BaseSettings) null;
    }

    public override void SaveSettings(BaseSettings settings)
    {
      foreach (ISettingsContainer settingsContainer in this._settingsContainers)
        settingsContainer.SaveSettings(settings);
      foreach (IExternalSettingsProvider settingsProvider in this._externalSettingsProviders)
        settingsProvider.SaveSettings(settings);
    }

    public override void ResetSettings(BaseSettings settings)
    {
      foreach (ISettingsContainerCanReset containerCanReset in this._settingsContainers.OfType<ISettingsContainerCanReset>())
        containerCanReset.ResetSettings(settings);
      foreach (IExternalSettingsProvider settingsProvider in this._externalSettingsProviders)
        settingsProvider.ResetSettings(settings);
    }

    public override void OverrideSettings(BaseSettings settings)
    {
      foreach (ISettingsContainerCanOverride containerCanOverride in this._settingsContainers.OfType<ISettingsContainerCanOverride>())
        containerCanOverride.OverrideSettings(settings);
      foreach (IExternalSettingsProvider settingsProvider in this._externalSettingsProviders)
        settingsProvider.OverrideSettings(settings);
    }

    public override IEnumerable<ISettingsPreset> GetPresets(string id)
    {
      foreach (ISettingsContainerPresets settingsContainer in this._settingsContainers.OfType<ISettingsContainerPresets>())
      {
        foreach (ISettingsPreset preset in settingsContainer.GetPresets(id))
          yield return preset;
      }
      foreach (IExternalSettingsProvider settingsProvider in this._externalSettingsProviders)
      {
        foreach (ISettingsPreset preset in settingsProvider.GetPresets(id))
          yield return preset;
      }
    }

    public override IEnumerable<UnavailableSetting> GetUnavailableSettings()
    {
      foreach (ISettingsContainerHasUnavailable settingsContainer in this._settingsContainers.OfType<ISettingsContainerHasUnavailable>())
      {
        foreach (UnavailableSetting unavailableSetting in settingsContainer.GetUnavailableSettings())
          yield return unavailableSetting;
      }
    }

    public override IEnumerable<SettingSnapshot> SaveAvailableSnapshots()
    {
      foreach (ISettingsContainerHasSettingsPack settingsContainer in this._settingsContainers.OfType<ISettingsContainerHasSettingsPack>())
      {
        foreach (SettingSnapshot snapshot in settingsContainer.SaveAvailableSnapshots())
          yield return snapshot;
      }
    }

    public override IEnumerable<BaseSettings> LoadAvailableSnapshots(
      IEnumerable<SettingSnapshot> snapshots)
    {
      foreach (ISettingsContainerHasSettingsPack settingsContainer in this._settingsContainers.OfType<ISettingsContainerHasSettingsPack>())
      {
        foreach (BaseSettings settings in settingsContainer.LoadAvailableSnapshots(snapshots))
          yield return settings;
      }
    }
  }
}
